package dsk.demo.product.controller;

import dsk.demo.common.utils.PageUtils;
import dsk.demo.product.dto.ModelQueryDto;
import dsk.demo.product.dto.ProductQueryDto;
import dsk.demo.product.entity.*;
import dsk.demo.product.service.*;
import dsk.demo.product.util.StrUtil;
import dsk.demo.product.vo.HotSellVo;
import dsk.demo.product.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/mobile")
public class MobilePageController {
    @Autowired
    private ProductService productService;
    @Autowired
    private GuanggaoService guanggaoService;
    @Autowired
    private HotSellService hotSellService;
    @Autowired
    private SearchService searchService;
    @Autowired
    private ContactService contactService;

    @Autowired
    private CatalogService catalogService;

    private static final String keywords = "Auto Parts, Auto sensors,Brake Pads, Cars,Truck parts, " +
            "Filters ,Oil FIlter,Air Filter, Fule Filter ,Air Dryer, Passenger car, Mercedes-Benz,TOYOTA, " +
            "FORD, BMW, LEXUS, VOLVO, MAZIDA, Renault, Hyundai, Suzuki, Audi, Mitsubishi, Nissan, Peugeot, Opel, BYD";

    /**
     * 首页
     *
     * @param model
     * @return
     */
    @RequestMapping({"/index", "/index.html", "/"})
    public String goToHome(Model model) {
        List<GuanggaoEntity> list = guanggaoService.list();
        model.addAttribute("guanggaoList", list);
        List<HotSellVo> hotSellProduct = hotSellService.getHotSellProduct();
        model.addAttribute("hotSellList", hotSellProduct);
        model.addAttribute("contact", contactService.getContact());
        Map<String, String> pageInf = new HashMap<>();
        pageInf.put("title", "YINPEI AUTO PARTS");
        pageInf.put("description", "YinPei Auto Parts. - A worldwide manufacturer  all kinds of air dryer,air, lube, fuel, coolant, transmission, and hydraulic filtration products.All kinds of sensors, break pads.");
        pageInf.put("keywords", keywords);
        model.addAttribute("pageInf", pageInf);

        List<CatalogEntity> catalogs = catalogService.selectAllChildNode("");
        model.addAttribute("catalogs", catalogs);
        return "mobile/index";
    }

    /**
     * 前端页面查询某一类型的 产品列表
     *
     * @param id
     * @param pageNum
     * @param limit
     * @param model
     * @return
     */
    @RequestMapping({"/page/catalog/{id}", "/page/catalog"})
    public String goToCatalog(@PathVariable(required = false) Long id, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "40") Integer limit, Model model) {
        ProductQueryDto productQueryDto = new ProductQueryDto();
        productQueryDto.setCatalogId(id);
        productQueryDto.setPage(pageNum);
        productQueryDto.setLimit(limit);
        PageUtils pageUtils = productService.queryPage(productQueryDto);
        List<ProductVo> list = (List<ProductVo>) pageUtils.getList();
        list.stream().forEach(item -> {
            item.dealReturnOemList();
        });
        model.addAttribute("productList", list);
        model.addAttribute("page", pageUtils);
        model.addAttribute("contact", contactService.getContact());
        int totalPage = pageUtils.getTotalPage();
        ArrayList<Integer> pageNums = new ArrayList<>();
        for (int i = 1; i <= totalPage; i++) {
            pageNums.add(i);
        }
        model.addAttribute("pageNums", pageNums);
        Map<String, String> pageInf = new HashMap<>();
        String enName = "";
        if (id != null && id > 0) {
            CatalogEntity byId = catalogService.getById(id);
            enName = byId.getCatalogEnname();
        }
        pageInf.put("title", enName + " CATALOG" + " YINPEI AUTO PARTS");
        pageInf.put("description", "YinPei Auto Parts. - A worldwide manufacturer  all kinds of air dryer,air, lube, fuel, coolant, transmission, and hydraulic filtration products.All kinds of sensors, break pads.");
        pageInf.put("keywords", keywords);
        model.addAttribute("pageInf", pageInf);

        List<CatalogEntity> catalogs = catalogService.selectAllChildNode("");
        model.addAttribute("catalogs", catalogs);
        return "mobile/catalog";
    }

    //搜索框返回结果
    @RequestMapping({"/page/search"})
    public String goToSearchOem(String foundString, Model model) {
        foundString = StrUtil.getFoundString(foundString);
        List<ProductVo> search = searchService.search(foundString);
        model.addAttribute("productList", search);
        model.addAttribute("contact", contactService.getContact());
        Map<String, String> pageInf = new HashMap<>();
        pageInf.put("title", "OE:" + foundString + " Search result");
        pageInf.put("description", "YinPei Auto Parts. - A worldwide manufacturer  all kinds of air dryer,air, lube, fuel, coolant, transmission, and hydraulic filtration products.All kinds of sensors, break pads.");
        pageInf.put("keywords", keywords);
        model.addAttribute("pageInf", pageInf);
        List<CatalogEntity> catalogs = catalogService.selectAllChildNode("");
        model.addAttribute("catalogs", catalogs);
        return "mobile/search";
    }

    //搜索框返回结果
    @RequestMapping({"/page/model"})
    public String goToSearchModel(String keyWord, Long catalogId, @RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer limit, Model model) {
        ModelQueryDto modelQueryDto = new ModelQueryDto(keyWord, catalogId, pageNum, limit);
        PageUtils pageUtils = searchService.searchByCarKeyWord(modelQueryDto);
        model.addAttribute("productList", pageUtils.getList());
        model.addAttribute("page", pageUtils);
        model.addAttribute("contact", contactService.getContact());
        Map<String, String> pageInf = new HashMap<>();
        pageInf.put("title", "KeyWord:" + keyWord + " Search result");
        pageInf.put("description", "YinPei Auto Parts. - A worldwide manufacturer  all kinds of air dryer,air, lube, fuel, coolant, transmission, and hydraulic filtration products.All kinds of sensors, break pads.");
        pageInf.put("keywords", keywords);
        model.addAttribute("pageInf", pageInf);

        List<CatalogEntity> catalogs = catalogService.selectAllChildNode("");
        model.addAttribute("catalogs", catalogs);
        return "mobile/model";
    }

    //产品详情页
    @RequestMapping({"/page/detail/{productId}"})
    public String goToDetail(@PathVariable(name = "productId") Long productId, Model model) {
        ProductVo detailById = productService.getDetailById(productId);
        List<ProductEntity> productEntities = productService.querySimilarProducts(detailById.getCatalogId(), productId);
        model.addAttribute("detail", detailById);
        model.addAttribute("contact", contactService.getContact());
        model.addAttribute("similarProducts", productEntities);

        Map<String, String> pageInf = new HashMap<>();
        String oemStr = "";
        int max = Math.min(10, detailById.getOemList().size());
        List<ProductRefEntity> oemList = detailById.getOemList();
        for (int i = 0; i < max; i++) {
            oemStr = oemStr + oemList.get(i).getBrand() + ":" + oemList.get(i).getRefNo() + ";";
        }

        String carStr = "";
        int maxCar = Math.min(10, detailById.getCarList().size());
        List<ProductCarEntity> carList = detailById.getCarList();
        for (int i = 0; i < maxCar; i++) {
            carStr = carStr + carList.get(i).getBrand() + ":" + carList.get(i).getFullname() + ";";
        }

        pageInf.put("title", detailById.getProductNo() + " " + detailById.getCatalogEnname() + " " + oemStr);
        pageInf.put("description", detailById.getProductNo() + " " + detailById.getCatalogEnname() + " " + oemStr + " APPLICATION:" + carStr);
        pageInf.put("keywords", keywords);
        model.addAttribute("pageInf", pageInf);

        List<CatalogEntity> catalogs = catalogService.selectAllChildNode("");
        model.addAttribute("catalogs", catalogs);
        return "mobile/detail";
    }

}
